Skip to content

Replace gorilla/mux with Go 1.22 stdlib ServeMux in test server#4955

Merged
pietern merged 3 commits intomainfrom
go1.22-replace-gorilla-mux-with-stdlib-servemux
May 6, 2026
Merged

Replace gorilla/mux with Go 1.22 stdlib ServeMux in test server#4955
pietern merged 3 commits intomainfrom
go1.22-replace-gorilla-mux-with-stdlib-servemux

Conversation

@pietern
Copy link
Copy Markdown
Contributor

@pietern pietern commented Apr 13, 2026

Summary

  • Remove the gorilla/mux dependency by switching libs/testserver to Go 1.22's enhanced net/http.ServeMux, which added method-based routing ("GET /items/{id}"), path wildcards ({id}), and rest wildcards ({path...}) — the same features we used gorilla for. See the Go 1.22 release notes.
  • Replace gorilla's {path:.*} regex wildcards with stdlib {path...} rest wildcards, and mux.Vars(r) with r.PathValue().
  • Exact (non-wildcard) paths are dispatched via a map lookup before reaching ServeMux. This avoids a ServeMux limitation where registering method-specific exact paths alongside method-specific wildcard patterns panics due to Go's implicit GET→HEAD handling creating unresolvable precedence. When an exact path has no handler for the request method, it falls through to ServeMux where a wildcard handler can serve it (e.g., a stub registers GET /exact/path, but HEAD falls through to a wildcard HEAD handler).
  • Wildcard patterns in test.toml stubs must now use the same placeholder names as the default handlers they coexist with — ServeMux panics on structurally identical patterns with different wildcard names. Two stubs updated: {name}{full_name}.
  • Extract the routing logic into a Router type with its own tests (libs/testserver/router.go + router_test.go); Server embeds it. The package doc on Router explains why the wrapper exists on top of stdlib ServeMux.

Test plan

  • Unit tests pass (libs/testserver, bundle/direct/dresources, acceptance/internal)
  • Acceptance selftests pass
  • Full CI

This pull request was AI-assisted by Isaac.

@pietern pietern requested a review from denik April 13, 2026 21:38
pietern added 2 commits May 6, 2026 10:16
Go 1.22 added method-based routing ("GET /items/{id}"), path wildcards
({id}), and rest wildcards ({path...}) to net/http.ServeMux, which
directly replaces the features we used gorilla/mux for. This removes the
gorilla/mux dependency from go.mod and NOTICE.

Exact (non-wildcard) paths are dispatched via a map lookup before
reaching ServeMux. This avoids a ServeMux limitation where registering
e.g. "GET /exact/path" alongside "HEAD /prefix/{path...}" panics because
Go's implicit GET→HEAD handling creates an unresolvable precedence
conflict. When an exact path is registered for one method but a request
arrives for a different method, it falls through to ServeMux where the
wildcard handler serves it.

Wildcard patterns in test stubs (test.toml) must use the same
placeholder names as the default handlers they coexist with. ServeMux
panics on structurally identical patterns with different names (e.g.
{name} vs {full_name}). Two test stubs are updated accordingly.

Co-authored-by: Isaac
Centralizes the method+path matching logic (exact routes, wildcard
delegation to ServeMux, vars extraction, percent-encoded slash
workaround) into a Router type with its own tests, and embeds it in
Server. The package doc on Router explains why the wrapper exists on
top of Go 1.22's ServeMux.

Co-authored-by: Isaac
@pietern pietern force-pushed the go1.22-replace-gorilla-mux-with-stdlib-servemux branch from e6b6f01 to 2db9663 Compare May 6, 2026 09:04
@pietern pietern temporarily deployed to test-trigger-is May 6, 2026 09:05 — with GitHub Actions Inactive
@pietern pietern temporarily deployed to test-trigger-is May 6, 2026 09:05 — with GitHub Actions Inactive
Use strings.SplitSeq and http.MethodGet/Head/Post constants to satisfy
the modernize and usestdlibvars linters.

Co-authored-by: Isaac
@pietern pietern temporarily deployed to test-trigger-is May 6, 2026 09:27 — with GitHub Actions Inactive
@pietern pietern temporarily deployed to test-trigger-is May 6, 2026 09:27 — with GitHub Actions Inactive
@pietern pietern merged commit 45222b5 into main May 6, 2026
29 of 30 checks passed
@pietern pietern deleted the go1.22-replace-gorilla-mux-with-stdlib-servemux branch May 6, 2026 11:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants